package com.hitqz.robot.biz.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.common.entity.PatrolPose;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.mapper.PatrolPoseMapper;
import com.hitqz.robot.biz.service.PatrolPoseService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Service
@AllArgsConstructor
public class PatrolPoseServiceIml extends ServiceImpl<PatrolPoseMapper, PatrolPose> implements PatrolPoseService {

    private final RobotEquipmentService robotEquipmentService;
    private final EquipmentFactory equipmentFactory;

    @Override
    public R executeAsync(PatrolPose patrolPose, Integer cmd) {
        RobotEquipment equipment = robotEquipmentService.getById(patrolPose.getEquipmentId());
        IEquipmentService service = equipmentFactory.getServiceByBrand(equipment.getBrand());
        if (service==null){
            return R.failed("找不到"+equipment.getName()+"驱动");
        }
        if (cmd == 0) {
            // 回正
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setPassword(equipment.getPassword());
            queryParamsDto.setUsername(equipment.getUsername());
            queryParamsDto.setPort(equipment.getPort());
            queryParamsDto.setChannel(equipment.getChannel());
            queryParamsDto.setIp(equipment.getIp());
            service.setEquipmentParams(queryParamsDto,
                    service.getDeviceParamsData(queryParamsDto, EquipmentCmdEnum.GET_ORIGIN_PARAMS), true);
        }
        if (cmd == 1) {
            //调用
            List<EquipmentParamsDto> paramsDtos = JSONArray.parseArray(patrolPose.getJson(), EquipmentParamsDto.class);
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setPassword(equipment.getPassword());
            queryParamsDto.setUsername(equipment.getUsername());
            queryParamsDto.setPort(equipment.getPort());
            queryParamsDto.setChannel(equipment.getChannel());
            queryParamsDto.setIp(equipment.getIp());
            service.setEquipmentParams(queryParamsDto, paramsDtos , true);
        }
        return R.ok();
    }
}
